<?php

class CategoryController extends Controller{
    /**
	 * @return array action filters
	 */
	public function filters(){
		return array(
			'accessControl', // perform access control for CRUD operations
			'postOnly + delete', // we only allow deletion via POST request
            'postOnly + lock',
		);
	}
    
    /**
	 * Specifies the access control rules.
	 * This method is used by the 'accessControl' filter.
	 * @return array access control rules
	 */
	public function accessRules(){
		return array(
            array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('delete'),
				'roles'=>array('admin'),
			),
			array('allow', // allow admin user to perform 'admin' and 'delete' actions
				'actions'=>array('create','update','index','lock'),
				'roles'=>array('admin'),
			),
			array('deny',  // deny all users
				'users'=>array('*'),
			),
            
		);
	}
    
	public function actionIndex(){
        $category = array(
            'blog'=>'文章分类',
            'file'=>'文件分类',
            'link'=>'链接分类',
            'user'=>'用户分类',
            'ad'=>'广告分类',
        );

        //文章的分类
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $criteria->condition = 'category = :category';
        $criteria->params = array(':category'=>'blog');
        $blogType = Type::model()->findAll($criteria);
        unset($criteria);

        //文件分类
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $criteria->condition = 'category = :category';
        $criteria->params = array(':category'=>'file');
        $fileType = Type::model()->findAll($criteria);
        unset($criteria);

        //链接分类
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $criteria->condition = 'category = :category';
        $criteria->params = array(':category'=>'link');
        $linkType = Type::model()->findAll($criteria);
        unset($criteria);

        //用户分类
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $criteria->condition = 'category = :category';
        $criteria->params = array(':category'=>'user');
        $userType = Type::model()->findAll($criteria);
        unset($criteria);
        
        //广告分类
        $criteria=new CDbCriteria();
        $criteria->order = 'create_date DESC';
        $criteria->condition = 'category = :category';
        $criteria->params = array(':category'=>'ad');
        $adType = Type::model()->findAll($criteria);
        unset($criteria);





        $this->render('index', array(
            'category'=>$category,
            'blogType'=>$blogType,
            'fileType'=>$fileType,
            'linkType'=>$linkType,
            'userType'=>$userType,
            'adType'=>$adType
        ));
	}
   
    
    /**
     * 创建分类
     */
    public function actionCreate(){
        $model = new Type('Insert');
        $message = array();
        
        if(Yii::app()->request->isPostRequest){
            $title = Yii::app()->request->getPost('title','');
            $alias = Yii::app()->request->getPost('alias','');
            $category = Yii::app()->request->getPost('category','');
            
            if(!empty($title)){
                $model->name = $title;
            }else{
                $message['title_error'] = '名称不能为空';
            }
            
            if(!empty($alias)){
                $model->screen_name = $alias;
            }else{
                $message['alias_error'] = '别名不能为空';
            }
            
            if(!empty($category)){
                $model->category = $category;
            }else{
                $message['category_error'] = '父类不能为空';
            }
            
            try{
                $model->setScenario('Insert');
                if($model->save()){
                    $message['status'] = 1;
                }else{
                    $message['status'] = 0;
                }
                
            }catch(Exception $e){
                $message['status'] = 0;
            }
            
            echo json_encode($message);
        }
    }
    
    /**
     * 更新
     * @param type $id
     */
    public function actionUpdate($id){
        $message = array();
        
        if(Yii::app()->request->isPostRequest){
        	$id = Yii::app()->request->getQuery('id','');
            $title = Yii::app()->request->getPost('title','');
            $alias = Yii::app()->request->getPost('alias','');
            
            if(empty($id)){
                $message['status'] = 0;
				$message['message_error'] = '修改的项目不存在';
				echo json_encode($message);
				exit;
            }
            
			$criteria = new CDbCriteria();
			$criteria->condition = 'id=:id';
			$criteria->params = array(':id'=>$id);
			$model = Type::model()->find($criteria);
			
            if(!empty($alias)){
                $model->screen_name = $alias;
            }
            
            if(!empty($title)){
                $model->name = $title;
            }
            
            try{
                if($model->save()){
                    $message['status'] = 1;
                }else{
                    $message['status'] = 0;
                }
                
            }catch(Exception $e){
                $message['status'] = 0;
            }
            
            echo json_encode($message);
        }
    }
    
    public function actionDelete(){
        $id = $_POST['id'];
        $model = $this->loadModel($id);
        $model->is_delete = (int)!$model->is_delete;
        $model->updateByPk($id,array('is_delete'=>$model->is_delete));
        $message = array();
        $message['code'] = 1;
        $message['status'] = $model->is_delete;
        echo json_encode($message);
    }
    
    public function actionLock(){
        $id = $_POST['id'];
        $model = $this->loadModel($id);
        $model->is_lock = (int)!$model->is_lock;
        $model->updateByPk($id,array('is_lock'=>$model->is_lock));
        $message = array();
        $message['code'] = 1;
        $message['lock'] = $model->is_lock;
        echo json_encode($message);
    }
    
    /**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
    public function loadModel($id){
		$model = Type::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'请求的页面不存在.');
		return $model;
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}
